﻿using QuanLyTiemVang.Classes;
using QuanLyTiemVang.DAO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace QuanLyTiemVang.GUI
{
    /// <summary>
    /// Interaction logic for QuiDinh.xaml
    /// </summary>
    public partial class QuiDinh : Window, IPhanQuyenHelper
    {
        public QuiDinh()
        {
            InitializeComponent();
            this.Loaded += QuiDinh_Loaded;
        }

        private void QuiDinh_Loaded(object sender, RoutedEventArgs e)
        {
            CurrentUser.Instance.AddForm(this);
            QUIDINH timeToLock = QuiDinhDAO.GetTimeToLock();
            QUIDINH maxDebt = QuiDinhDAO.GetMaxDebt();
            QUIDINH maxTimePay = QuiDinhDAO.GetMaxTimePay();
            if (timeToLock == null||maxDebt==null||maxTimePay==null)
                MessageBox.Show("Chưa thêm dữ liệu vào bảng qui định!");
            else
            {
                txtTimeToLock.Text = timeToLock.GiaTri.ToString();
                txtMaxDept.Text = maxDebt.GiaTri.ToString();
                txtMaxDebtTime.Text = maxTimePay.GiaTri.ToString();
            }
        }

        private void Change_Policy_HandlerEvent(object sender, RoutedEventArgs e)
        {
            QUIDINH qd = new QUIDINH();
            qd.TenQuyDinh = "ThoiGianKhoa";
            qd.GiaTri = Int32.Parse(txtTimeToLock.Text);
            QuiDinhDAO.ChangePolicy(qd, "ThoiGianKhoa");
            if (OnChangeTimeToLock != null)
                OnChangeTimeToLock();
            MessageBox.Show("Thay đổi thành công!");
        }

        private void btnThayDoiMaxDebt_Click_1(object sender, RoutedEventArgs e)
        {
            QUIDINH qd = new QUIDINH();
            qd.TenQuyDinh = "NoToiDa";
            qd.GiaTri = Int32.Parse(txtMaxDept.Text);
            QuiDinhDAO.ChangePolicy(qd, "NoToiDa");
            MessageBox.Show("Thay đổi thành công!");
        }

        private void btnThayDoiDebtTime_Click_1(object sender, RoutedEventArgs e)
        {
            QUIDINH qd = new QUIDINH();
            qd.TenQuyDinh = "ThoiGianTraChamToiDa";
            qd.GiaTri = Int32.Parse(txtMaxDebtTime.Text);
            QuiDinhDAO.ChangePolicy(qd, "ThoiGianTraChamToiDa");
            MessageBox.Show("Thay đổi thành công!");
        }

        private void Quit_Click_EventHandler(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
        {
            base.OnMouseLeftButtonDown(e);
            this.DragMove();
        }

        // textbox number only
        private void PreviewTextInput_TextBox_Event_Handler(object sender, TextCompositionEventArgs e)
        {
            CheckIsNumeric(e);
        }
        private void CheckIsNumeric(TextCompositionEventArgs e)
        {
            int result;
            if (!(int.TryParse(e.Text, out result)))
                e.Handled = true;
        }

        // event change policy
        public delegate void ChangePolicy();
        public event ChangePolicy OnChangeTimeToLock;

        void IPhanQuyenHelper.SetAdminFunction()
        {
            this.btnThayDoi.IsEnabled = true;
            this.txtTimeToLock.IsEnabled = true;
        }

        void IPhanQuyenHelper.SetStaffFunction()
        {
            this.btnThayDoi.IsEnabled = false;
            this.txtTimeToLock.IsEnabled = false;
        }

        void IPhanQuyenHelper.SetCustomerFunction()
        {
            this.Close();
        }

        void IPhanQuyenHelper.SetNonFunction()
        {
            this.Close();
        }        
    }
}
